package com.corpgovernment.organization.service;

import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.corpgovernment.api.organization.model.org.request.GetUserConfigRequest;
import com.corpgovernment.api.organization.model.org.request.SaveUserConfigRequest;
import com.corpgovernment.api.organization.model.org.response.GetUserConfigResponse;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.organization.entity.db.MbUserConfig;
import com.corpgovernment.organization.mapper.MbUserConfigMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员配置信息
 * @author xiaoxiaoguo
 */
@Slf4j
@Service
public class UserConfigServiceImpl implements IUserConfigService {

    @Autowired
    private MbUserConfigMapper userConfigMapper;

    @Override
    public void saveUserConfig(SaveUserConfigRequest request) {

        MbUserConfig userConfig = new MbUserConfig();
        userConfig.setUserId(request.getUserId());
        userConfig.setAgentMessageConfig(request.getAgentMessageConfig());

        if(hasUserConfig(request.getUserId())) {
            // 更新
            Example userConfigExample = new Example(MbUserConfig.class);
            userConfigExample.createCriteria().andEqualTo("userId", request.getUserId())
                    .andEqualTo("isDeleted", 0);
            userConfigMapper.updateByExampleSelective(userConfig, userConfigExample);
        } else {
            // 新增
            userConfigMapper.insertSelective(userConfig);
        }
    }

    /**
     * 是否有用户配置
     */
    private boolean hasUserConfig(String uId) {
        MbUserConfig userConfig = new MbUserConfig();
        userConfig.setUserId(uId);
        userConfig.setIsDeleted(false);
        return userConfigMapper.selectCount(userConfig) > 0;
    }

    @Override
    public GetUserConfigResponse getUserConfig(GetUserConfigRequest request) {
        MbUserConfig userConfig = new MbUserConfig();
        userConfig.setUserId(request.getUserId());
        userConfig.setIsDeleted(false);
        List<MbUserConfig> userConfigList = userConfigMapper.select(userConfig);
        if(CollectionUtils.isEmpty(userConfigList)) {
            return null;
        }
        if(userConfigList.size()>1) {
            log.info("用户配置有效数据不止一条，请检查");
        }

        GetUserConfigResponse configResponse = new GetUserConfigResponse();
        configResponse.setAgentMessageConfig(userConfigList.get(0).getAgentMessageConfig());
        return configResponse;
    }

    @Override
    public void deleteUserConfig(List<String> userIds) {
        if(CollectionUtils.isEmpty(userIds)) {
            return;
        }
        // 构建查询条件
        Example example = new Example(MbUserConfig.class);
        example.createCriteria().andEqualTo("isDeleted", 0).andIn("userId", userIds);

        // 设置更新值
        MbUserConfig record = new MbUserConfig();
        record.setIsDeleted(true);

        userConfigMapper.updateByExampleSelective(record, example);
    }

    @Override
    public List<GetUserConfigResponse> findUserConfigByUserIds(List<String> userIdList) {
        // 构建查询条件
        Example example = new Example(MbUserConfig.class);
        example.createCriteria().andEqualTo("isDeleted", 0).andIn("userId", userIdList);
        List<MbUserConfig> userConfigList = userConfigMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(userConfigList)) {
            return null;
        }
        List<GetUserConfigResponse> result = new ArrayList<>();
        for(MbUserConfig MbUserConfig:userConfigList) {
            GetUserConfigResponse configResponse = new GetUserConfigResponse();
            configResponse.setUserId(MbUserConfig.getUserId());
            configResponse.setAgentMessageConfig(MbUserConfig.getAgentMessageConfig());
            result.add(configResponse);
        }
        return result;
    }
}
